class Video < ActiveRecord::Base
  has_many              :blog_posts
  has_many              :comments
  belongs_to            :show
  belongs_to            :song
  belongs_to            :blog_post
  validates_presence_of :name
  validates_presence_of :link
  validates_presence_of :description
  validates_presence_of :caption
  
  before_save           :youtube_thumbnail
  
  before_destroy        :delete_comments
  
  has_friendly_id       :name, :use_slug => true
  
  def youtube_thumbnail
    self.thumbnail = "http://img.youtube.com/vi/" + self.youtube_id + "/default.jpg"
  end
  
  def youtube_id
    return self.link.scan(/youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)[&\w;=\+_\-]*/).first.first
  end
protected
  def delete_comments
    self.comments.each {|comment| comment.destroy}
  end
end
